package com.linkage.seapeak.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.util.Log;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CommonUtils  {

    public static int dp2px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    /**
     * 传入为long型时间
     * @param timeLong
     * @return
     */
    public static String resetTime(long timeLong) {
        String time = "";
        String formatTemp = "yyyy-MM-dd HH:mm:ss";
        try {
            SimpleDateFormat format = new SimpleDateFormat(formatTemp);
            Date date = new Date(timeLong);
            time = format.format(date);
            return resetTime(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 重置金额
     * @param input
     * @return
     */
    public static String resetMoney(String input) {
        try {
            double doubleInput = Double.parseDouble(input);


            return resetMoney(doubleInput);
        } catch (Exception e) {
            e.printStackTrace();
            return input;
        }
    }

    double stt = 999999999999.3344;
    DecimalFormat format = new DecimalFormat("#,##0.00");//不以科学计数法显示，并把结果用逗号隔开保留两位小数
    BigDecimal bigDecimal = new BigDecimal(stt);


    public static String resetMoney_nodot(String inputString) {

        if("".equalsIgnoreCase(inputString) )
        {
            inputString = "0";
        }
        String money="0.00";
        try {
             double input = Double.parseDouble(inputString);
            if(input== 0.00)
            {
                return "0.00";
            }
            DecimalFormat format = new DecimalFormat("##0.00");//不以科学计数法显示，并把结果用逗号隔开保留两位小数

            money= format.format(input);
            return money;
        } catch (Exception e) {
            e.printStackTrace();
            return "" + money;
        }
    }

    public static String resetMoney_nodot(double input) {
        try {
            if(input== 0.00)
            {
                return "0.00";
            }
            DecimalFormat format = new DecimalFormat("##0.00");//不以科学计数法显示，并把结果用逗号隔开保留两位小数

            String money= format.format(input);
            return money;
        } catch (Exception e) {
            e.printStackTrace();
            return "" + input;
        }
    }
    public static String resetMoney(double input) {
        try {

            if(input== 0.00)
            {
                return "0.00";
            }
            DecimalFormat format = new DecimalFormat("#,##0.00");//不以科学计数法显示，并把结果用逗号隔开保留两位小数

            String money= format.format(input);
            return money;
        } catch (Exception e) {
            e.printStackTrace();
            return "" + input;
        }
    }


    public static boolean vailMoney(Activity context, String money)
    {
        boolean flag = false;

        if("".equalsIgnoreCase(money))
        {
            money = "0.00";
        }
        try{
            double d_money = Double.parseDouble(money);
            if(d_money >999999999999.99 || d_money<0)
            {
                flag = false;
                ToastUtil.getInstance(context).showToast("金额操过范围", com.linkage.seapeak.utils.ToastUtil.TOAST_TYPE.WARNING);
            }else{
                flag = true;
            }
        }catch (Exception ex)
        {
            ex.printStackTrace();
        }
        return flag;
    }



    /**
     * 传入为long型时间
     * @param timeLong
     * @return
     */
    public static String resetTime(String timeLong,String format_time) {
        String time = "";
        String formatTemp = "yyyy-MM-dd HH:mm:ss";
        if(!"".equalsIgnoreCase(format_time))
        {
            formatTemp = format_time;
        }
        try {
            SimpleDateFormat format = new SimpleDateFormat(formatTemp);
            Date date = new Date(timeLong);
            time = format.format(date);
            return resetTime(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return time;
    }

    /**
     * 只显示时间：时，分	wf/df所有应用的创建或编辑时间为当天
     * 除设有样式显示条件的时间显示
     * 显示昨天+时间：昨天，时，分	今天之前的数据，时间字段显示为“昨天”
     * 显示日期+时间：月，日，时，分	昨天以前的，显示日期+时间
     * 显示年+时间：年，月，日，时，分	在当年的前一年，显示年份+月+日+时+分
     *
     * @param time 格式为 yyyy-MM-dd HH:mm:ss 或者 yyyy-MM-dd HH:mm
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String resetTime(String time) {
        try {
            String tempTime = "";
            if (time.split(":").length == 3)
                tempTime = time.substring(0, time.length() - 3);
            else
                tempTime = time;

            String currentTimeStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            String recTimeStr = tempTime.split(" ")[0];
            String[] recTime = recTimeStr.split("-");
            String[] currentTime = currentTimeStr.split("-");

            if (recTimeStr.equals(currentTimeStr)) {
                long sysTime = System.currentTimeMillis();
                long setTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time).getTime();
//                Log.i("zhong", "resetTime: " + sysTime + " | " + setTime);
                if ((sysTime - setTime) > (60 * 60 * 1000)) {
                    int hour = (int) ((sysTime - setTime) / (60 * 60 * 1000) + 1);
                    return hour + "小时前";
                } else if (sysTime - setTime > 60 * 1000) {
                    int minute = (int) ((sysTime - setTime) / (60 * 1000) + 1);
                    return minute + "分钟前";
                } else if (sysTime - setTime > 1000) {
                    int minute = (int) ((sysTime - setTime) / 1000 + 1);
                    return minute + "秒前";
                } else {
                    return "现在";
                }
            } else if (recTime[0].equals(currentTime[0])) {
                int offsetDay = offsetDay(time);
                if (offsetDay == -1)
                    return "昨天 " + time.split(" ")[1];
                if (offsetDay == -2)
                    return "两天前 " + time.split(" ")[1];
                if (offsetDay == -3)
                    return "三天前 " + time.split(" ")[1];
                if (offsetDay == -4)
                    return "四天前 " + time.split(" ")[1];
                if (offsetDay == -5)
                    return "五天前 " + time.split(" ")[1];
                if (offsetDay == -6)
                    return "六天前 " + time.split(" ")[1];
                return time.substring(5); //同年不显示年
            } else
                return time; //年份不同
        } catch (Exception e) {
            e.printStackTrace();
            return time;
        }
    }

    /**
     * 判断是否为昨天(效率比较高)
     *
     * @param day 传入的 时间  "2016-06-28 10:10:30" "2016-06-28" 都可以
     * @return true今天 false不是
     * @throws ParseException
     */
    public static int offsetDay(String day) throws ParseException {
        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = format.parse(day);
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);
            return diffDay;
        }
        return 0;
    }
}
